import 'package:flutter/material.dart';
import 'package:zhihuishop/pages/cart/index.dart';
import 'package:zhihuishop/pages/category/index.dart';
import 'package:zhihuishop/pages/home/index.dart';

import 'mine/index.dart';

/**
 * 智慧商城首页底部tab
 */
class TabBarPage extends StatefulWidget {
  const TabBarPage({super.key});

  @override
  State<TabBarPage> createState() => _TabBarPageState();
}

class _TabBarPageState extends State<TabBarPage> {
  // 当前选中索引
  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
          child: IndexedStack(
        index: _currentIndex,
        children: [
          HomePage(),
          CategoryPage(),
          CartPage(),
          MinePage(),
        ],
      )),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        items: barItemList(),
        // items列表
        currentIndex: _currentIndex,
        selectedItemColor: Colors.red,
        unselectedItemColor: Colors.black,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
    );
  }

  List<BottomNavigationBarItem> barItemList() {
    List<BottomNavigationBarItem> items = [];
    items.add(
      BottomNavigationBarItem(
          icon: Image.asset(
            'assets/tabbar/home.png',
            width: 32,
            height: 32,
          ),
          activeIcon: Image.asset(
            'assets/tabbar/home-active.png',
            width: 32,
            height: 32,
          ),
          label: '首页'),
    );
    items.add(
      BottomNavigationBarItem(
          icon: Image.asset(
            'assets/tabbar/cate.png',
            width: 32,
            height: 32,
          ),
          activeIcon: Image.asset(
            'assets/tabbar/cate-active.png',
            width: 32,
            height: 32,
          ),
          label: '分类'),
    );
    items.add(
      BottomNavigationBarItem(
          icon: Image.asset(
            'assets/tabbar/cart.png',
            width: 32,
            height: 32,
          ),
          activeIcon: Image.asset(
            'assets/tabbar/cart-active.png',
            width: 32,
            height: 32,
          ),
          label: '购物车'),
    );
    items.add(
      BottomNavigationBarItem(
          icon: Image.asset(
            'assets/tabbar/user.png',
            width: 32,
            height: 32,
          ),
          activeIcon: Image.asset(
            'assets/tabbar/user-active.png',
            width: 32,
            height: 32,
          ),
          label: '我的'),
    );
    return items;
  }
}
